package com.fs.controller;

import com.fs.annotation.SysLog;
import com.fs.pojo.SysAdmin;
import com.fs.service.SysAdminService;
import com.fs.vo.PageResult;
import com.fs.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

//@RefreshScope
@Api(tags = "管理员")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private SysAdminService adminService;

    @SysLog("查看管理员列表")
    @ApiOperation(value = "查看管理员列表")
    @RequiresPermissions({"admin:list"})
    @GetMapping("/list")
    public SysResult adminList(PageResult requestVO){
        return adminService.adminList(requestVO);
    }

    /**
     * 管理员登陆错误次数后解锁
     * @param id
     * @return
     */
    @SysLog("管理员登陆错误次数后解锁")
    @ApiOperation(value = "管理员登陆错误次数后解锁")
    @RequiresPermissions({"admin:UnLock"})
    @PutMapping("/unlockAdmin/{id}")
    public SysResult adminUnLock(@PathVariable("id") Integer id){
        return adminService.unLock(id);
    }

    /**
     * 添加管理员信息
     * @param admin: username,password
     * @return
     */
    @SysLog("添加管理员信息")
    @ApiOperation(value = "添加管理员信息")
    @RequiresPermissions({"admin:add"})
    @PostMapping("/add")
    public SysResult addAdmin(@RequestBody SysAdmin admin){
        return adminService.addAdmin(admin);
    }

    /**
     * 修改管理员密码
     * @param admin : id,password,username
     * @return
     */
    @SysLog("修改管理员密码")
        @ApiOperation(value = " ")
    @RequiresPermissions({"admin:pwdAdmin"})
    @PutMapping("/edit")
    public SysResult pwdAdmin(@RequestBody SysAdmin admin){
        return adminService.pwdAdmin(admin);
    }

    /**
     * 启用禁用管理员
     * @param id
     * @param status
     * @return
     */
    @SysLog("启用禁用管理员")
    @ApiOperation(value = "启用禁用管理员")
    @RequiresPermissions({"admin:enableAdmin"})
    @PutMapping("/status/{id}/{status}")
    public SysResult enableAdmin(@PathVariable("id") Integer id,@PathVariable("status") Boolean status){
        return adminService.enableAdmin(id,status);
    }

    /**
     * 获取单个管理员信息
     * @param id
     * @return
     */
    @SysLog("获取单个管理员信息")
    @ApiOperation(value = "获取单个管理员信息")
    @RequiresPermissions({"admin:getById"})
    @GetMapping("/get/{id}")
    public SysResult getById(@PathVariable("id") Integer id){
        return adminService.getById(id);
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @SysLog("删除管理员")
    @ApiOperation(value = "删除管理员")
    @RequiresPermissions({"admin:del"})
    @DeleteMapping("/{id}")
    public SysResult delById(@PathVariable("id") Integer id){
        return adminService.delById(id);
    }

//    @GetMapping("/menu")
//    public ResultVO adminMenu(@RequestHeader("Cookie") String cookie){
//        ResultVO checkLogin = checkLogin(cookie);
//        if(checkLogin.getStatus()!=0){
//            return checkLogin;
//        }
//        return adminService.adminMenu(Long.valueOf(checkLogin.getData().toString()));
//    }

    @GetMapping("/menu")
//    public ResultVO adminMenu(@RequestHeader("Cookie") String cookie){
    public SysResult adminMenu(){
        return adminService.adminMenu(1L);
    }
}
